package com.gxa.cddx.www.spting_ai.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxa.cddx.www.springboot_ai_2.dto.StudentQueryDto;
import com.gxa.cddx.www.springboot_ai_2.mapper.StudentMapper;
import com.gxa.cddx.www.springboot_ai_2.pojo.Student;
import com.gxa.cddx.www.springboot_ai_2.util.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author liye
 * @date 2025/10/16 16:51
 * @description:
 */
@RestController
@RequestMapping("/student")
@Tag(name = "学生管理")
public class StudentController {
    //把数据库操作层注入进入  叫把spring boot容器管理的对象 赋值给这个属性  =new StudentMapper()
    @Resource
    private StudentMapper studentMapper;

    @GetMapping("/list")
    @Operation(summary = "查询所有学生")
    public R list(){
        //return R.ok(studentMapper.list());
        return R.ok(studentMapper.selectList(null));
    }

    //按条件分页查询 StudentQueryDto
    @GetMapping("/listByPage")
    @Operation(summary = "按条件分页查询")
    public R listByPage(StudentQueryDto studentQueryDto){
            IPage<Student> page = new Page<>(studentQueryDto.getPage(),studentQueryDto.getPageSize());
//            page  =studentMapper.listByPage(studentQueryDto);
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<Student>();
        //判断如果clazzId 不为空  则添加查询班级条件
        if(studentQueryDto.getClazzId()!=null ){
            queryWrapper.eq(Student::getClazzId,studentQueryDto.getClazzId());
        }
        //判断如果id 不为空  则添加查询id条件
        if(studentQueryDto.getId()!=null && studentQueryDto.getId().length()>0){
            queryWrapper.eq(Student::getId,studentQueryDto.getId());
        }
        //判断如果phone 不为空  则添加查询手机号条件
        if(studentQueryDto.getPhone()!=null && studentQueryDto.getPhone().length()>0){
            queryWrapper.eq(Student::getStuPhone,studentQueryDto.getPhone());
        }
        //判断如果idcared 不为空  则添加查询身份证条件
        if(studentQueryDto.getIdcared()!=null && studentQueryDto.getIdcared().length()>0){
            queryWrapper.eq(Student::getStuIdcared,studentQueryDto.getIdcared());
        }
        //判断如果name 不为空  则添加查询姓名条件
        if(studentQueryDto.getName()!=null && studentQueryDto.getName().length()>0){
            queryWrapper.like(Student::getName,studentQueryDto.getName());
        }

        //查询
        List<Student> list = studentMapper.selectList(page, queryWrapper);
        //装载
        page.setRecords(list);
        //获取总记录数
        System.out.println(page.getCurrent());
        //获取当前页数据
        System.out.println(page.getRecords());
        return R.ok(page);
    }


    @GetMapping("/add")
    @Operation(summary = "添加学生")
    public R add(Student student){
       // return studentMapper.addStudent(student)>0?R.ok():R.error();
        return studentMapper.insert(student)>0?R.ok():R.error();
    }
    @GetMapping("/update")
    @Operation(summary = "修改学生")
    public R update(Student student){
        //return studentMapper.updateStudent(student)>0? R.ok():R.error();
        return studentMapper.updateById(student)>0? R.ok():R.error();
    }
    @GetMapping("/delete")
    @Operation(summary = "删除学生")
    public R delete(String id) {
        //return studentMapper.deleteStudent(id) > 0 ? R.ok() : R.error();
        return studentMapper.deleteById(id) > 0 ? R.ok() : R.error();
    }

}
